/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.apache.hadoop.contrib.mfs;

import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hadoop.contrib.mfs.Chunk;

/**
 *
 * @author eces
 */
public class TestMetadataManager {

    public static MetadataManager manager;

    public static void main(String args[]) {
        MfsConfiguration.config.setProperty("mfs.storage.totalSize", Integer.toString(1024*1024*50) );
        MfsConfiguration.config.setProperty("mfs.storage.chunkSize", Integer.toString(1024*1024) );

        System.out.println(MfsConfiguration.config.toString());
        
        manager = MetadataManager.getInstance();

        System.err.println("STORAGE: " + (manager.getTotalStorageSize()-manager.getFreeStorageSize()) + " / " + manager.getTotalStorageSize());
        System.out.println("File Info Gathering");
        FileInfo f = new FileInfo();
        f.setFileName("test1");
        f.setPath("/base/");
        f.setSize(1024*1024*3);
        System.err.println(f.getAbsolutePath());


        List<Chunk> chunks = null;
        System.out.println("Chunk Preparation");
        try {
            chunks = manager.allocateChunks(f.getSize());
        } catch (InsufficientSpaceException ex) {
            Logger.getLogger(TestMetadataManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Prepation for Writing. (read-only work may not require this process)");
        manager.markChunks(chunks);

        System.out.println("Writing at block");
        Iterator<Chunk> iter = chunks.iterator();
        while(iter.hasNext()){
            Chunk c = iter.next();
            System.out.printf("Allocated chunk's seq: %d [%d]\n", c.seq, c.flag);
        }

        f.setChunkList(chunks);
        System.out.println("Finished and release flag");
        manager.unmarkChunks(chunks);

        System.out.println("Add new file: test1");
        manager.add(f);

        System.err.println("STORAGE: " + (manager.getTotalStorageSize()-manager.getFreeStorageSize()) + " / " + manager.getTotalStorageSize());

        System.out.println("Find the file which entitled /base/test_not_created");
        try {
            manager.find("/base/test_not_created");
            //manager.add(null);
        } catch (FileNotFoundException ex) {
            System.out.println("OK - File is Not FOUND");
        }

        System.out.println("Find the file which entitled /base/test1");
        try {
            FileInfo newFile = manager.find("/base/test1");
            
            List<Chunk> newChunks = newFile.getChunkList();
            Iterator<Chunk> newIter = newChunks.iterator();
            while(newIter.hasNext()){
                Chunk c = newIter.next();
                System.out.printf("Allocated chunk's seq: %d [%d]\n", c.seq, c.flag);
            }
        } catch (FileNotFoundException ex) {
            System.err.println("Error: " + ex.getMessage());
            Logger.getLogger(TestMetadataManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        
        System.err.println("STORAGE: " + (MetadataManager.getTotalStorageSize()-MetadataManager.getFreeStorageSize()) + " / " + MetadataManager.getTotalStorageSize());


        System.out.println("List Files...");
        FileInfo[] files = manager.list("/base");
        System.err.println(files.length + " file(s) found");
        for(int i=0; i<files.length; i++){
            System.out.println("\t" + files[i].getFileName());
        }
        System.err.println("STORAGE: " + (MetadataManager.getTotalStorageSize()-MetadataManager.getFreeStorageSize()) + " / " + MetadataManager.getTotalStorageSize());

        System.out.println("Delete /base/test1");
        try {
            manager.delete("/base/test1");
        } catch (FileNotFoundException ex) {
            System.err.println("Error: " + ex.getMessage());
            Logger.getLogger(TestMetadataManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("List Files...");

        FileInfo[] newFiles = manager.list("/base");
        System.err.println(newFiles.length + " file(s) found");
        for(int i=0; i<newFiles.length; i++){
            System.out.println("\t" + newFiles[i].getFileName());
        }

        System.err.println("STORAGE: " + (manager.getTotalStorageSize()-manager.getFreeStorageSize()) + " / " + manager.getTotalStorageSize());
        System.out.println("Test Complete.");
    }


}
